<?php
using("Libs_Catalog");
using("Tests_Databases_Catalog");
using("Db_Pdo_ResultSet");

/**
 * Remember to use the pdo driver for ADODB and SQLite as the native SQLite
 * driver does not support sqlite version 3.0
 * 
 * This test assumes that more than one element exists in the CustomAttributes
 * table.  This is to avoid having the testIterator method pass on an empty
 * result set.
 */
class Tests_Db_Pdo_PdoResultSetTest extends UnitTestCase {
	
	/**
	 * PDO recordset
	 *
	 * @var PDoRecordSet
	 */
	private $_recordset;
	
	private static $_query = "SELECT * FROM CustomAttributes";
	
	public function __construct() {
		$dbh = new PDO("sqlite:" . Tests_Databases_Catalog::fetch("test.db"));
		$this->_recordset = $dbh->query(Tests_Db_Pdo_PdoResultSetTest::$_query);
	}
	
	public function testConstruct() {
		$resultset = new Db_Pdo_ResultSet($this->_recordset, Tests_Db_Pdo_PdoResultSetTest::$_query);
		$this->assertTrue(is_a($resultset, "Db_Pdo_ResultSet"));
	}
	
	public function testQuery() {
		$resultset = new Db_Pdo_ResultSet($this->_recordset, Tests_Db_Pdo_PdoResultSetTest::$_query);
		$this->assertTrue($resultset->getQuery() == Tests_Db_Pdo_PdoResultSetTest::$_query);
	}
	
	public function testIterator() {
		try {
			$resultset = new Db_Pdo_ResultSet($this->_recordset, Tests_Db_Pdo_PdoResultSetTest::$_query);
			$iterator = $resultset->getIterator();
			$results = array();
			foreach ($resultset as $result) {
				$results[] = $result;
			}
			$this->assertTrue(is_a($iterator, "Traversable"));		
			$this->assertTrue(count($results) > 1);
		} catch (Exception $e) {
			$this->fail($e->getMessage());
		}
	}
}
?>